package vsvm.gui.forms;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import vsvm.data.filter.ranking.rankingStuff.Normalization;

import java.awt.Insets;
import javax.swing.JCheckBox;

public class NormalizationForm extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JButton btnOk = null;
	private JButton jButton = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JTextField txtMin = null;
	private JTextField txtMax = null;
	
	private NormalizationForm me = null;
	private Normalization normalization = null;
	private JCheckBox checkBox = null;

	/**
	 * @param owner
	 */
	public NormalizationForm(Frame owner, Normalization normalization) {
		super(owner);
		me = this;
		this.normalization = normalization;
		initialize();
		this.setLocationRelativeTo(owner);
		this.setVisible(true);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setTitle("Normalization form");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.SOUTH);
			jContentPane.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(15);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jPanel = new JPanel();
			jPanel.setLayout(flowLayout);
			jPanel.add(getBtnOk(), null);
			jPanel.add(getJButton(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes btnOk	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setText("   Ok   ");
			btnOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if(checkBox.isSelected())
					{
						try 
						{
							normalization.setMin(Double.parseDouble(txtMin.getText()));
						} 
						catch (NumberFormatException e1) 
						{
							JOptionPane.showMessageDialog(me, "Incorrect minimum value!", "Error", 0);
							return;
						}
						try 
						{
							normalization.setMax(Double.parseDouble(txtMax.getText()));
						} 
						catch (NumberFormatException e1) 
						{
							JOptionPane.showMessageDialog(me, "Incorrect maximum value!", "Error", 0);	
							return;
						}
						normalization.setNormalize(true);
						dispose();	
					}
					else
					{
						normalization.setNormalize(false);
						dispose();	
					}

				}
			});
		}
		return btnOk;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Cancel");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints7.gridy = 0;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.BOTH;
			gridBagConstraints6.gridy = 2;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.insets = new Insets(0, 20, 5, 20);
			gridBagConstraints6.gridx = 2;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.BOTH;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(0, 20, 5, 20);
			gridBagConstraints5.gridx = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.insets = new Insets(0, 10, 5, 10);
			gridBagConstraints4.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("Max");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.insets = new Insets(0, 10, 5, 10);
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			jLabel1 = new JLabel();
			jLabel1.setText("Min");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.insets = new Insets(0, 10, 0, 10);
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Normalization");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.fill = GridBagConstraints.NONE;
			gridBagConstraints1.insets = new Insets(0, 10, 0, 0);
			gridBagConstraints1.gridy = 0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(jLabel, gridBagConstraints2);
			jPanel1.add(jLabel1, gridBagConstraints3);
			jPanel1.add(jLabel2, gridBagConstraints4);
			jPanel1.add(getTxtMin(), gridBagConstraints5);
			jPanel1.add(getTxtMax(), gridBagConstraints6);
			jPanel1.add(getCheckBox(), gridBagConstraints7);
		}
		return jPanel1;
	}

	/**
	 * This method initializes txtMin	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtMin() {
		if (txtMin == null) {
			txtMin = new JTextField();
			txtMin.setText(String.valueOf(normalization.getMin()));
		}
		return txtMin;
	}

	/**
	 * This method initializes txtMax	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtMax() {
		if (txtMax == null) {
			txtMax = new JTextField();
			txtMax.setText(String.valueOf(normalization.getMax()));
		}
		return txtMax;
	}

	/**
	 * This method initializes checkBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getCheckBox() {
		if (checkBox == null) {
			checkBox = new JCheckBox();
			if(normalization.isNormalize())
			{
				checkBox.setSelected(true);
			}
		}
		return checkBox;
	}

}
